/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.gravitino.trino.connector;

import io.trino.spi.connector.BucketFunction;
import io.trino.spi.connector.ConnectorBucketNodeMap;
import io.trino.spi.connector.ConnectorNodePartitioningProvider;
import io.trino.spi.connector.ConnectorPartitioningHandle;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.ConnectorSplit;
import io.trino.spi.connector.ConnectorTransactionHandle;
import io.trino.spi.type.Type;
import java.util.List;
import java.util.Optional;
import java.util.function.ToIntFunction;

/**
 * This class provides a ConnectorNodePartitioningProvider for Trino to decide data partitioning
 * strategy.
 */
public class GravitinoNodePartitioningProvider implements ConnectorNodePartitioningProvider {

  ConnectorNodePartitioningProvider nodePartitioningProvider;

  /**
   * Constructs a new GravitinoNodePartitioningProvider with the specified node partition provider.
   *
   * @param nodePartitioningProvider the internal connector node partition provider
   */
  public GravitinoNodePartitioningProvider(
      ConnectorNodePartitioningProvider nodePartitioningProvider) {
    this.nodePartitioningProvider = nodePartitioningProvider;
  }

  @Override
  public Optional<ConnectorBucketNodeMap> getBucketNodeMapping(
      ConnectorTransactionHandle transactionHandle,
      ConnectorSession session,
      ConnectorPartitioningHandle partitioningHandle) {
    return nodePartitioningProvider.getBucketNodeMapping(
        GravitinoHandle.unWrap(transactionHandle),
        session,
        GravitinoHandle.unWrap(partitioningHandle));
  }

  @Override
  public ToIntFunction<ConnectorSplit> getSplitBucketFunction(
      ConnectorTransactionHandle transactionHandle,
      ConnectorSession session,
      ConnectorPartitioningHandle partitioningHandle) {
    return nodePartitioningProvider.getSplitBucketFunction(
        GravitinoHandle.unWrap(transactionHandle),
        session,
        GravitinoHandle.unWrap(partitioningHandle));
  }

  @Override
  public BucketFunction getBucketFunction(
      ConnectorTransactionHandle transactionHandle,
      ConnectorSession session,
      ConnectorPartitioningHandle partitioningHandle,
      List<Type> partitionChannelTypes,
      int bucketCount) {
    return nodePartitioningProvider.getBucketFunction(
        GravitinoHandle.unWrap(transactionHandle),
        session,
        GravitinoHandle.unWrap(partitioningHandle),
        partitionChannelTypes,
        bucketCount);
  }
}
